$(function () {
    let query = {
        pagenum: 1,
        pagesize: 3,
        cate_id: '',
        state: ''
    }

    let layer = layui.layer
    initArticleList()
    function initArticleList() {
        axios({
            method: 'GET',
            url: '/my/article/list',
            params: query
        }).then(res => {
            if (res.data.status != 0) {
                return layer.msg(res.data.message)
            }
            // console.log(res.data);
            let arr = []
            res.data.data.forEach(ele => {
                arr.push(`
                <tr>
              <td>${ele.title}</td>
              <td>${ele.cate_name}</td>
              <td>${dateFormat(ele.pub_date)}</td>
              <td>${ele.state}</td>
              <td>
                <a href='/article/art_edit.html?id=${ele.Id}'  class="layui-btn layui-btn-xs">
                  编辑
                </a>
                <button
                   data-id=${ele.Id}
                  class=" btn-delete layui-btn layui-btn-xs layui-btn-danger"
                >
                  删除
                </button>
              </td>
            </tr>
                `)
            })
            let str = arr.join('')
            $('tbody').empty().html(str)
            pageRender(res.data.total)
        })
    }
    let form = layui.form
    initArtCateList();
    function initArtCateList(){
        axios({
            method:'GET',
            url: '/my/article/cates',
            
        }).then(res=>{
            if(res.data.status!==0){
                return layer.msg(res.data.message)
            }
            let arr = ['<option value="">请选择文章类别</option>']
            res.data.data.forEach(ele=>{
                arr.push(`
                
                <option value="${ele.Id}">${ele.name}</option>
                `)
            })
            let str = arr.join("")
            $("[name=cate_id]").empty().html(str)
           form.render()
        })
    }

    $('#formSearch').on('submit',function(e){
        e.preventDefault()
        query.cate_id=$('[name=cate_id]').val()
        query.state=$('[name=state]').val()
        initArticleList();
    })
    var laypage = layui.laypage;
    function pageRender(total){
        laypage.render({
            elem: 'pageBox' //注意，这里的 test1 是 ID，不用加 # 号
            ,count: total, //数据总数，从服务端得到
            limit:query.pagesize,
            curr:query.pagenum,
            layout:['count','limit','prev','page','next','skip'],
            limits:[2,3,5,10],
            jump:function(obj,first){
                if(!first){
                    query.pagenum=obj.curr
                    query.pagesize=obj.limit
                    initArticleList()
                }
            }
          
          });
    }
    $('tbody').on('click',".btn-delete",function(){
        let id = $(this).attr('data-id')
        layer.confirm('确认删除此文章吗？',{icon:3,title:"提示"},function(index){
            axios({
                method:'GET',
                url:'/my/article/delete/'+id
            }).then(res=>{
                if(res.data.status !=0){
                    return layer.msg(res.data.message)
                }
                layer.msg('恭喜您，删除文字成功！')
                if($('.btn-delete').length==1&& query.pagenum>=2){
                    query.pagenum--
                }
                initArticleList()
            })
            layer.close(index)
        })
    })
})
